package com.zhkj.shixi.tvplayer.VideoPlayer;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.zhkj.shixi.tvplayer.Interface.VideoFullScreen;
import com.zhkj.shixi.tvplayer.MyMediaPlayer;

/**
 * Created by suqiuyu on 19-8-16.
 */

public class VideoFull implements VideoFullScreen {
    private boolean isFull = true;
    private MyMediaPlayer mPlayer;
    private SurfaceView mView;
    private int mHeight;
    private int mWidth;

    public VideoFull(MyMediaPlayer player, SurfaceView view, Activity activity) {
        mPlayer = player;
        mView = view;
        WindowManager mWindowManager;
        mWindowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        mHeight = mWindowManager.getDefaultDisplay().getHeight();
        mWidth = mWindowManager.getDefaultDisplay().getWidth();
        Log.i("VideoFull: ", "heigt: " + mHeight + ";width: " + mWidth);
    }

    @Override
    public void setVideoFullScreen() {
        if (isFull) {
            int videoWidth = mPlayer.getVideoWidth();
            int videoHeight = mPlayer.getVideoHeight();
            Log.i("setVideoFull: ", "heigt: " + videoHeight + ";width: " + videoWidth);
            float max = Math.max((float) videoWidth / (float) mWidth,(float) videoHeight / (float) mHeight);
            videoWidth = (int) Math.ceil((float) videoWidth / max);
            videoHeight = (int) Math.ceil((float) videoHeight / max);
            ViewGroup.LayoutParams lp = mView.getLayoutParams();
            lp.width = videoWidth;
            lp.height = videoHeight;
            mView.setLayoutParams(lp);
            isFull = false;
        } else {
            setFull();
        }
    }

    public void setFull () {
        ViewGroup.LayoutParams lp = mView.getLayoutParams();
        lp.width = mWidth;
        lp.height = mHeight;
        mView.setLayoutParams(lp);
        isFull = true;
    }

}
